var express = require("express");
var router = express.Router();
var { data } = require("./product");

let codeList = [];

let users = [
  {
    token: "cdaa36",
    phone: 13868170715,
    username: "会员715",
    avatar:
      "https://img1.baidu.com/it/u=2510172678,1291663481&fm=253&fmt=auto&app=138&f=JPEG?w=400&h=400",
    cart: [],
    addresses: [],
  },
  {
    token: "f1ac38",
    phone: 13868170716,
    username: "会员716",
    avatar:
      "https://img1.baidu.com/it/u=2510172678,1291663481&fm=253&fmt=auto&app=138&f=JPEG?w=400&h=400",
    cart: [],
    addresses: [],
  },
];

// 发短信验证码
router.post("/code", function (req, res, next) {
  const { phone } = req.body;

  if (!phone) {
    res.json({
      success: false,
      msg: "请输入手机号",
    });
  } else {
    const reg =
      /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[1589]))\d{8}$/;
    if (reg.test(+phone)) {
      const code = Math.floor(Math.random() * 9000) + 1000;

      const findIndex = codeList.findIndex((item) => {
        return item.phone === +phone;
      });
      if (findIndex >= 0) {
        codeList = codeList
          .filter((item) => item.phone !== +phone)
          .concat({ phone: +phone, code });
      } else {
        codeList.push({ phone: +phone, code });
      }

      res.json({
        success: true,
        code,
      });
    } else {
      res.json({
        success: false,
        msg: "手机号格式错误",
      });
    }
  }
});

// 登录和注册
router.post("/login", function (req, res, next) {
  const { phone, code } = req.body;

  if (!phone || !code) {
    res.json({
      success: false,
      msg: "参数错误",
    });
  } else {
    const item = codeList.find((item) => item.phone === +phone);
    if (!item) {
      res.json({
        success: false,
        msg: "手机号错误",
      });
    } else {
      if (item.code === +code) {
        codeList = codeList.filter((item) => item.phone !== +phone);
        const i = users.findIndex((item) => item.phone === +phone);
        if (i >= 0) {
          res.json({
            success: true,
            msg: "登录成功",
            token: users[i].token,
          });
        } else {
          const token = Math.floor(Math.random() * 0xffffff)
            .toString(16)
            .padEnd(6, "0");
          users.push({
            token,
            phone: +phone,
            username: "会员" + (+phone % 10000),
            avatar:
              "https://img1.baidu.com/it/u=2510172678,1291663481&fm=253&fmt=auto&app=138&f=JPEG?w=400&h=400",
            cart: [],
            addresses: [],
          });
          console.log(users);

          res.json({
            success: true,
            msg: "登录成功",
            token,
          });
        }
      } else {
        res.json({
          success: false,
          msg: "验证码错误",
        });
      }
    }
  }
});

// 获取个人信息
router.get("/info", function (req, res, next) {
  const token = req.headers.token;

  if (!token) {
    res.json({
      success: false,
      msg: "token错误",
    });
  } else {
    const item = users.find((item) => item.token === token);

    if (item) {
      res.json({
        success: true,
        info: {
          phone: item.phone,
          username: item.username,
          avatar: item.avatar,
        },
      });
    } else {
      res.json({
        success: false,
        msg: "token错误",
      });
    }
  }
});

// 获取购物车信息
router.get("/cart", function (req, res, next) {
  const token = req.headers.token;

  if (!token) {
    res.json({
      success: false,
      msg: "token错误",
    });
  } else {
    const item = users.find((item) => item.token === token);
    if (item) {
      res.json({
        success: true,
        data: item.cart,
      });
    } else {
      res.json({
        success: false,
        msg: "token错误",
      });
    }
  }
});

// 添加至购物车
router.get("/addcart", function (req, res, next) {
  const token = req.headers.token;
  const { id, count } = req.query;

  if (!token) {
    res.json({
      success: false,
      msg: "token错误",
    });
  } else {
    if (!id || !count) {
      res.json({
        success: false,
        msg: "参数错误",
      });
    } else {
      const cart = users.find((item) => item.token === token).cart;
      const product = data.find((item) => item.id === +id);

      const i = cart.findIndex((item) => item.id === +id);
      if (i >= 0) {
        cart[i] = { ...cart[i], count: cart[i].count + +count };
      } else {
        cart.push({
          id: product.id,
          product_name: product.product_name,
          product_img: product.product_img,
          product_price: product.product_price,
          product_vip_price: product.product_price,
          count: +count,
        });
      }

      res.json({
        success: true,
        msg: "添加成功",
      });
    }
  }
});

// 后台获取所有用户
router.get("/list", function (req, res, next) {
  const token = req.headers.token;
  const { username } = req.query;

  if (token !== "821cef") {
    res.json({
      success: false,
      msg: "token错误",
    });
  } else {
    if (username) {
      const data = users.filter((item) => {
        return item.username.includes(username);
      });
      res.json({
        success: true,
        data: data,
      });
    } else {
      res.json({
        success: true,
        data: users,
      });
    }
  }
});

module.exports = router;
